<template>
  <div id="check">
    <div class="check-container" id="prtTable">
      <el-form ref="form" label-width="150px" >
        <el-form-item label="选择请验单版本">
          <el-select v-model="reportVersion" placeholder="请选择" :clearable="true" style="width: 400px;">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
        </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="download" >下载</el-button>
            <el-button type="primary" :loading="makePdf" @click="view" >预览</el-button>
          </el-form-item>
      </el-form>

    </div>
  </div>
</template>

<script>

  import {
   printCheckView,
   getPrintModelList
  } from "@/api/print"

  import {
    CODE_OK
  } from '@/common/js/config'

  export default {
    data() {
      return {
        visible: false,
        options:[],
        codes:["",""],
        makePdf:false,
        reportVersion:"",
        viewUrl:"",
        downloadUrl:""
      }
    },
    props: ["childDataForm"],
    created() {
      this.getOptions();
    },
    methods: {
      download(){
        window.open(this.downloadUrl, '_blank')
      },
      getOptions(){
        const params = {
          page: 1,
          limit: 10000,
          clazz: 'wkqy'
        }
          getPrintModelList(params).then(res=>{
           let data = res.page.list;
           for(let i in data){
             let option = {"label":data[i].modelCode,"value":data[i].modelCode};
             this.options.push(option);
           };
           this.reportVersion = this.options[this.options.length-1].label;
          }).catch();
      },
      view(){
        this.makePdf = true;
          if(this.reportVersion !== ""){
            //预览
            let param = {
              checkId:this.childDataForm.checkId,
              checkMergeId:this.childDataForm.checkIdcheckMergeId,
              content:"yfb",
              printCode:this.reportVersion
            }
            printCheckView(param).then(res=>{
            	if (res.code === CODE_OK) {
                  this.$message({
                    message: '创建成功',
                    type: 'success',
                    duration: 3000
                  });
                  this.makePdf = false;
                  this.downloadUrl = res.downloadUrl;
                  window.open(res.viewUrl, '_blank')
            	}else{
            		this.$message.error(res.msg);
                this.makePdf = false;
            	}
            }).catch()
          }else{
            this.makePdf = false;
          }
      },
      subDate(str) {
        if (str == undefined || str == null) {
          return '';
        } else {
          return formatDate(str);
        }
      },
      close() {
        this.visible = false
        this.$emit('close')
      },
      /**
       * 是否为空字符串，全空格也是空字符串
       * @param str
       * @returns {Boolean}
       */
      isBlank(obj) {
        if (obj == null || typeof obj == "undefined" ||
          obj == "") {
          return true;
        }
        return false;
      }
    }
  }
</script>

<style>
</style>
